package com.guessdroid.UI;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.guessdroid.R;
import com.guessdroid.labeling.GContext;

public class GContextAdapter extends ArrayAdapter<GContext> {

    private ArrayList<GContext> items;
    private LayoutInflater inflater;

    public GContextAdapter(Context context, int textViewResourceId, ArrayList<GContext> items, LayoutInflater inflater) {
            super(context, textViewResourceId, items);
            this.items = items;
            this.inflater = inflater;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            if (view == null) {
                view = inflater.inflate(R.layout.list_item, null);
            }
            GContext gContext = items.get(position);
            if (gContext != null) {
                    setValueForTextView(view, R.id.name, gContext.label);
                    setValueForTextView(view, R.id.contextSuccess, gContext.successPercent()+"%");
            }
            return view;
    }
    
    private void setValueForTextView(View view, int id, String value){
    	TextView element = (TextView) view.findViewById(id);
        if (element != null) {
              element.setText(value);
        }    
    }

}